package persentation.presentStudent;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import vo.chooseCourseVO.ChooseCourseVO;
import vo.courseVO.CourseVO;
import businesslogic.chooseCourseBl.ChooseCourse;
import businesslogic.chooseCourseBl.ChooseCourseController;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ListSelectionModel;

public class ShowCoursePanel extends JPanel {
	
	private ChooseCourseController chooseCourseController;
	private ArrayList<CourseVO> courseVOs;
	private JTable table;
	private DefaultTableModel model;
	private MyTabbedPane tabbedPane;
	

	public ShowCoursePanel(ChooseCourseController chooseCourseController) {
		this.chooseCourseController=chooseCourseController;
		
		
		setSize(800, 600);
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 780, 404);
		this.add(scrollPane);
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null,null,null,null},
				{null, null,null,null,null}
			},
			new String[] {
					 "课程编号","课程名","任课老师","上课时间","学分"
			}
		) 
		{
			Class[] columnTypes = new Class[] {
				String.class, String.class,String.class,String.class,String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		table.setBounds(119, 424, 262, 143);
		scrollPane.setViewportView(table);

		table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		JButton button = new JButton("查看课程详细信息");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CourseVO courseVO=courseVOs.get(table.getSelectedRow());
				
				tabbedPane.addOne(courseVO.getCourseName()+" 详细信息",new InfoPanel(courseVO));
				
			}
		});
		button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button.setBounds(288, 453, 161, 40);
		add(button);
		fillTable();

	}
	
	public void fillTable(){
		try {
			courseVOs = chooseCourseController.getMyCourse();
		} catch (Exception e) {
			
			e.printStackTrace();
			ExceptionHandler.networkException();
		}
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		tableModel.setRowCount(0);
		for (CourseVO course : courseVOs) {
			String[] array = new String[5];
			 array[0]=course.getCourseId();
			 array[1]=course.getCourseName();
			 array[2]=course.getTeacherName();
			 array[3]=course.getCourseTime();
			 array[4]=String.valueOf(course.getCredit());
			 tableModel.addRow(array);
			 table.invalidate();
			 table.updateUI();
	      }
	} 
	
	public void setTabbedPane(MyTabbedPane tabbedPane){
		this.tabbedPane=tabbedPane;
	}

	
	

}
